let express =require('express')
let cookieParser =require('cookie-parser')
let bodyParser =require('body-parser')
let mongoose =require('mongoose')

let apiAddr=require('./config/PublicPath')

//创建连接数据库
mongoose.connect("mongodb://127.0.0.1:27017/blogDbB",{
    useUnifiedTopology:true,
    useNewUrlParser:true,
}).then(res=>{
    console.log("连接数据库成功")
}).catch(err=>{
    console.log("连接数据库失败")
})

//实例
let app=express()


//启用中安渐渐
app.use(cookieParser())
app.use(bodyParser.urlencoded({extend:false}))
app.use(bodyParser.json())




//处理跨域
app.all("*",(req,res,next)=>{
    res.setHeader("Access-Control-Allow-Origin","*")
    res.setHeader("Access-Control-Allow-Methods","GET,POST,DELETE,OPTIONS")
    res.setHeader("Access-Control-Allow-Headers","X-Requested-With,Content-Type,Authorization")
    res.setHeader("Access-Control-Expose-Headers","Authorization")
    next()
})



const {getImgApp}=require("./service/img.file.get.service")
const {uploadImgFileApp}=require("./service/img.file.upload.service")
const {authenticationApp}=require('./service/identifyAuthenticationService')
const {blogApp} = require("./service/blogService");
const {userDetailApp} = require("./service/userDetailService");
const {tipOffApp} = require("./service/TipOffService");



app.use(apiAddr.getImgApiAddr,getImgApp)
app.use(apiAddr.uploadImgApiAddr,uploadImgFileApp)
app.use(apiAddr.identifyAuthenticationApiAddr,authenticationApp)
app.use(apiAddr.blogApiAddr,blogApp)

app.use(apiAddr.userDetailApiAddr,userDetailApp);
app.use(apiAddr.tipOffApiAddr,tipOffApp);






app.listen(8888)

console.log("8888端口启动")
// 创建聊天室的功能
const WebSocket = require('ws');

const server = new WebSocket.Server({ port: 12581 });
let userList=[]
server.on("connection",function (socketUser){
    userList.push(socketUser)
    socketUser.on("message",(data)=>{
        //给其他发送消息
        for (let i=0;i<userList.length;i++){
            if (socketUser !== userList[i]){
                userList[i].send(data)
                console.log("发送广播")
            }
        }
    })
    socketUser.on('close', () => {
        console.log('socketUser client disconnected');
    });
})
